package pl.drypisy;

import java.util.ArrayList;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

/**
 * shows route list from db
 * user can start route or see more about it
 * @author Michal Drobniak
 */

public class RouteListActivity extends Activity {
	
	DBAdapter db;
	
	ListView routeList;		//routes list from db
	ArrayAdapter<RouteData> routeListAdapter; //list adapter
	
	private Dialog aboutDialog;	//route decription
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.route_list_activ);
        
        db=new DBAdapter(this); // Database
        
        //route list
        routeList = (ListView) findViewById(R.id.routeList);
        routeList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				view.showContextMenu();
			}
		});
        
        //PRINTS ROUTE LIST
        printRoutes(); //<------------
        
    }
    
    /**
     * prints routes list
     * opens db
     */
    private void printRoutes(){
    	db.open();
        Cursor c = db.getAllRoutes();
        
        ArrayList<RouteData> routes=new ArrayList<RouteData>();
        
        if(c.moveToFirst()){
        	do { //id,title,date,about
        		routes.add(new RouteData(Integer.parseInt(c.getString(0)),
        				c.getString(1), c.getString(3) ,c.getString(2))); 
        	} while(c.moveToNext());
        }
        db.close();
        //inflater
        LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //set adapter to list
        routeListAdapter = new RouteListAdapter(getApplicationContext(), R.layout.route_list, routes,vi);
        routeList.setAdapter(routeListAdapter);
        registerForContextMenu(routeList);
    }
    	
    
    /*============CONTEXT MENU=============*/
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    	super.onCreateContextMenu(menu, v, menuInfo);
    	menu.add(0,0,0,R.string.routeListContextMenuOptionStart);
    	menu.add(0,1,1,R.string.routeListContextMenuOptionAbout);
    }
    
    public boolean onContextItemSelected(MenuItem item){
    	return routeChoiceMenu(item);
    }
    
    /*
     * Choice menu
     *  0 - start route
     *  1 - more about route
     */
    public boolean routeChoiceMenu(MenuItem item){
    	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    	View itemView=info.targetView;
    	int routeId=(Integer) info.targetView.getTag();
    	
    	switch(item.getItemId()){
	    	case 0:
				Intent intent=new Intent("pl.drypisy.MAP");
	    		Bundle extras = new Bundle();
	    		extras.putInt("rid", routeId);
	    		intent.putExtras(extras);
	    		startActivity(intent);
	    		return true;
	    	case 1:
	    		setAboutDialog(itemView);
	    		aboutDialog.show();
	    		return true;
    	}
    	return false;
    }

    /**
     * shows dialog with full description of route
     */
    private void setAboutDialog(View itemView){
    	TextView aboutView=(TextView) itemView.findViewById(R.id.routeAbout);
    	TextView titleView=(TextView) itemView.findViewById(R.id.routeTitle);
    	
    	aboutDialog=new Dialog(RouteListActivity.this);
		aboutDialog.setContentView(R.layout.about_dialog);
		aboutDialog.setTitle(titleView.getText());
		
		TextView aboutDialogAbout=(TextView) aboutDialog.findViewById(R.id.aboutDialogAbout);
		aboutDialogAbout.setText(aboutView.getText());
		Button aboutDialogBtn=(Button) aboutDialog.findViewById(R.id.aboutDialogOk);
		aboutDialogBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				aboutDialog.cancel();
			}
		});
    }
    
    
}